package com.litong.litongjiaoyu.controller;

import com.alibaba.fastjson.JSONObject;
import com.litong.litongjiaoyu.annotation.IgnoreAuth;
import com.litong.litongjiaoyu.entity.UserWage;
import com.litong.litongjiaoyu.entity.WxMaTemplateData;
import com.litong.litongjiaoyu.entity.WxMssVo;
import com.litong.litongjiaoyu.service.CommenService;
import com.litong.litongjiaoyu.service.WageService;
import com.litong.litongjiaoyu.utils.JSONResult;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName WageController
 * @Description 工资中心管理层
 * @Author jingz
 * @Date 2019/11/13 20:29
 * @Version 1.0
 **/

@RestController
@RequestMapping("/wage")
@Log4j2
public class WageController {

    @Autowired
    private WageService wageService;
    @Autowired
    private WxPushServiceQcl wxPushServiceQcl;

    @Autowired
    private CommenService commenService;

    /**
     * 添加工资信息，并发送
     * @param wage
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    public JSONResult add(@RequestBody JSONObject jsonObject) {

        UserWage wage = jsonObject.getObject("wage", UserWage.class);

        Map<String, WxMaTemplateData> data = new HashMap<String, WxMaTemplateData>(3);

        WxMaTemplateData wxMaTemplateData1 = new WxMaTemplateData();
        wxMaTemplateData1.setValue(wage.getTypes());
        data.put("thing1", wxMaTemplateData1);

        WxMaTemplateData wxMaTemplateData2 = new WxMaTemplateData();
        wxMaTemplateData2.setValue("￥" + String.valueOf(wage.getAmount()));
        data.put("amount3", wxMaTemplateData2);

        WxMaTemplateData wxMaTemplateData3 = new WxMaTemplateData();
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        wxMaTemplateData3.setValue(df.format(date));

        data.put("date2", wxMaTemplateData3);


        WxMssVo wxMssVo = new WxMssVo();
        wxMssVo.setTouser(commenService.getOpenId(wage.getPhone()));
        wxMssVo.setTemplate_id("EB8VHgOmgnFnhjE-cPMV5sHIiudgHsXtn9eyqEy7fZM");
        wxMssVo.setPage("pages/wage/Viewsalary");
        wxMssVo.setData(data);

//        获取消息发送结果
        JSONObject result = wxPushServiceQcl.pushOneUser("wx6a41faf108b8ecab", "6e9c02ef2d83dcd91a2f0e730a9aa358", wxMssVo);

        if (wageService.add(wage) == 1) {
            if (result.getInteger("errcode") == 0) {
                return JSONResult.ok();
            } else {
                return JSONResult.errorMsg(result.toString());
            }
        } else {
            return JSONResult.errorException("fail");
        }

//        判断是否发送成功，如果发送成功保存至数据库
//        若没有，返回错误信息

    }

    @ResponseBody
    @PostMapping("/update")
    public JSONResult update(@RequestBody UserWage wage) {
        if (wageService.update(wage) == 1) {
            return JSONResult.ok();
        } else {
            return JSONResult.errorMsg("");
        }
    }

    @IgnoreAuth
    @ResponseBody
    @GetMapping("/query")
    public JSONResult query() {
        return JSONResult.ok(wageService.query());
    }

    @IgnoreAuth
    @ResponseBody
    @PostMapping("/getbyid")
    public JSONResult getById(String id) {
        return JSONResult.ok(wageService.getById(Long.parseLong(id)));
    }

    @IgnoreAuth
    @ResponseBody
    @PostMapping("/getbyopenid")
    public JSONResult getByOpenId(String openid) {
        return JSONResult.ok(wageService.getByOpenId(openid));
    }

    @ResponseBody
    @PostMapping("/remove")
    public JSONResult remove(String id) {
        if (wageService.remove(Long.parseLong(id)) == 1) {
            return JSONResult.ok("success");
        } else {
            return JSONResult.errorMsg("fail");
        }
    }

    @ResponseBody
    @PostMapping("/batchRemove")
    public JSONResult batchRemove(String[] ids) {
        long[] id = (long[]) ConvertUtils.convert(ids, long.class);

        if (wageService.batchRemove(id) > 0) {
            return JSONResult.ok("success");
        } else {
            return JSONResult.errorMsg("fail");
        }
    }

}
